package com.gitee.wsl.compose.base

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.stringResource
import kotlin.jvm.JvmInline

@Immutable
sealed interface TextResource {

    @JvmInline
     value class Dynamic( val value: String) : TextResource

     class Resource(
        internal val id: StringResource,
        internal vararg val args: Any
    ) : TextResource {

        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (other !is Resource) return false

            if (id != other.id) return false
            if (!args.contentEquals(other.args)) return false

            return true
        }

        override fun hashCode(): Int {
            var result = id.hashCode()
            result = 31 * result + args.contentHashCode()
            return result
        }
    }

     companion object {

         operator fun invoke(value: String): Dynamic = Dynamic(value)
         operator fun invoke(id: StringResource, vararg args: Any): Resource = Resource(id, args = args)
    }
}

fun StringResource.text(vararg args: Any): TextResource.Resource = TextResource.Resource(this, args = args)


@Composable
fun TextResource.string(): String = when (this) {
    is TextResource.Dynamic -> value
    is TextResource.Resource -> stringResource(id, formatArgs = args)
}